/*****************************
 *@file: BaseBuff
 *@author: 陈吕唐
 *@desc: 基础BUFF组件 
 *@date: 2024-03-21	10:25
 *****************************/
import { _decorator, Component, error } from 'cc';
import { BaseCharacter } from '../BaseCharacter';
import IBaseBuff from './IBaseBuff';
import IBaseBuffDTO from './IBaseBuffDTO';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('BaseBuff')
@requireComponent(BaseCharacter)
export class BaseBuff extends Component implements IBaseBuff {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/

    /**
     * 当前BUff是否生效中
     */
    private _activeBuff: boolean = false;

    /**
     * 剩余时间
     */
    private _downTime: number = 0;

    /**
     * 值
     */
    private _value: number = 0;

    /***************************************************************
    *PropertyUI 引用的控件
    ***************************************************************/
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected lateUpdate(dt: number): void {
        if (!this.activeBuff) {
            return;
        }

        this.downTime -= dt;
    }

    protected onDisable(): void {
        this.activeBuff = false;
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /**
     * 启用BUFF
     * @param value 
     */
    public applyBuff(value: IBaseBuffDTO): void {
        this._activeBuff = true;
        this._downTime = value.time;
        this._value = value.value;
        this.enabled = true;
    }

    /**
     * 移除BUFF
     */
    protected removeBuff(): void {
        this.enabled = false;
    }
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
    public get activeBuff(): boolean {
        return this._activeBuff;
    }

    protected set activeBuff(value: boolean) {
        if (this._activeBuff == value) {
            return;
        }

        if (value) {
            error("BaseBuff.activeBuff: 启用BUFF失败");
            error("应当调用applyBuff方法")
            return;
        }

        this._activeBuff = value;

        if (!value) {
            this.removeBuff();
        }
    }

    /**
     * BUFF剩余持续时间
     */
    private get downTime(): number {
        return this._downTime;
    }
    private set downTime(value: number) {
        this._downTime = value;
        if (this._downTime <= 0) {
            this.enabled = false;
        }
    }

    protected get value(): number {
        return this._value;
    }
    protected set value(value: number) {
        this._value = value;
    }

    protected get character(): BaseCharacter {
        return this.node.getComponent(BaseCharacter);
    }

}


